<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\GoodsTable;
use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\StoreCombination;
use App\Admin\Renderable\StoreDetails;
use App\Models\GrGood;
use App\Models\StorePink;
use App\Models\Vip;
use App\service\ReviewImages;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\FormStep\Form as StepForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Alert;

class StoreCombinationController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new StoreCombination(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('product_id', '商品名称')->display(function ($e) {
                $data = GrGood::where('goods_id', $this->product_id)->pluck('goods_name');
//                dd($data->toArray());
                return ($data->toArray()[0]);
            })->modal(function ($modal) {
                $modal->title('商品');
                return StoreDetails::make(['product_id' => $this->product_id]);
            });

            $grid->column('image')->image('', 100, 100);
            $grid->column('title');
            $grid->column('price');
            $grid->column('people');
            $grid->column('participants');
            $grid->column('lump_num');
            $grid->column('quota_show');
            $grid->column('stop_time');
            $grid->column('quota');
            $grid->column('quota_show');
            $grid->column('is_show')->switch('');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->equal('is_show', '拼团状态')->select([
                    0 => '已结束',
                    1 => '进行中',
                ])->width(3);
            });


        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new StoreCombination(), function (Show $show) {
            $show->field('id');
            $show->field('product_id');
            $show->field('mer_id');
            $show->field('image');
            $show->field('images');
            $show->field('title');
            $show->field('attr');
            $show->field('people');
            $show->field('info');
            $show->field('price');
            $show->field('sort');
            $show->field('sales');
            $show->field('stock');
            $show->field('add_time');
            $show->field('is_host');
            $show->field('is_show');
            $show->field('is_del');
            $show->field('combination');
            $show->field('mer_use');
            $show->field('is_postage');
            $show->field('postage');
            $show->field('start_time');
            $show->field('stop_time');
            $show->field('effective_time');
            $show->field('cost');
            $show->field('browse');
            $show->field('unit_name');
            $show->field('temp_id');
            $show->field('weight');
            $show->field('volume');
            $show->field('num');
            $show->field('once_num');
            $show->field('quota');
            $show->field('quota_show');
            $show->field('virtual');
            $show->field('logistics');
            $show->field('freight');
            $show->field('custom_form');
            $show->field('virtual_type');
            $show->field('is_commission');
            $show->field('head_commission');
        });
    }

    protected $type = [
        1 => '固定邮费',
        2 => '运费模板'
    ];

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new StoreCombination(), function (Form $form) {
            $form->title('添加拼团商品');
            $form->display('id');
            // 第一列占据1/2的页面宽度
            $form->column(6, function (Form $form) {
                $form->selectTable('product_id', '选择商品：')
                    ->title('商品')
                    ->from(GoodsTable::make())
                    ->model(GrGood::class, 'goods_id', 'goods_img_cover', 'goods_type_name');

                $form->image('image', '商品主图：')->uniqueName()->autoUpload()->saveFullUrl();
                $form->multipleFile('images', '商品轮播图：')->uniqueName()->autoUpload()->saveFullUrl()->saving(function ($paths) {
//                         转化为json
                    return json_encode($paths);
                });
                $form->text('title', '拼团名称：')->default('')->maxLength(100, '不能超过100个字符');
                $form->textarea('info', '拼团简介：');
                $form->datetimeRange('start_time', 'stop_time', '拼团时间：')->help('设置活动开启结束时间，用户可以在设置时间内发起参与拼团');
                $form->checkbox('logistics', '物流方式：')->options([1 => '快递', 2 => '到店核销'])->saving(function ($v) {
                    return implode(',', $v);
                });

            });

// 第二列占据1/2的页面宽度
            $form->column(6, function (Form $form) {
                $form->decimal('price');
                $form->radio('freight_set', '运费设置：')
                    ->when([1, 3], function (Form $form) {
                        $form->number('postage', '运费:')->type('number')->help('');
                    })
                    ->when(2, function (Form $form) {
                        $form->text('temp_id', '运费模板ID：')->type('number')->help('');
                    })
                    ->options($this->type)
                    ->default(1);
                $form->text('effective_time', '拼团时效(单位 小时)：')->width(10, 2);
                $form->text('people', '拼团人数：')->help('单次拼团需要参与的用户数');

//                $form->text('num', '总购买数量限制：');
                $form->text('once_num', '单次购买数量限制：')->help('用户参与拼团时，一次购买最大数量限制。');
                $form->text('quota', '商品数量限制：');
                $form->text('unit_name', '单位：')->default('件', true);
                $form->number('head_commission', '团长返佣比例：')->help('拼团成功后，如果团长是分销员，则在订单确认收货时会给团长返一定的佣金，佣金比例是实际支付金额的0-100%');
                $form->number('sort', '排序：');
                $form->radio('is_host', '热门推荐：')->options([0 => '开启', 1 => '关闭']);
                $form->radio('is_show', '活动状态：')->options([0 => '开启', 1 => '关闭']);
            });
            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form) {
                // 判断是否是新增操作
                if (!$form->isCreating()) {
                    $newId = $form->getKey();
                    $data = StorePink::where('id', $newId)->where('status', 1)->get()->toArray();
                    if (!empty($data)) {
                        return $form->response()->error('正在进行拼团');
                    }
                    return;
                } else {
                    $img = $form->image;
                    if (!empty($img)) {
                        $reviewImage = new ReviewImages();
                        $res = $reviewImage->verify($img);
                        if ($res != "合规") {
                            return $form->response()->error('图片不合规');
//                            $form->responseValidationMessages()
                        }
                    }
                }
            });


        });
    }

}
